#include "stdafx.h"
#include "cef_wallet.h"
#include "module\cef\cef_global_methods.h"
using namespace ui;
using namespace std;

CefWallet::CefWallet()
{
	cef_control_ = nullptr;
}

void CefWallet::DoInit()
{
	cef_control_ = static_cast<CefControl*>(FindSubControl(L"cef_control"));
	cef_control_->AttachMenuCommand(nbase::Bind(&CefWallet::OnMenuCommand, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachLinkClick(nbase::Bind(&CefWallet::OnLinkClick, this, std::placeholders::_1));
	cef_control_->AttachBeforeNavigate(nbase::Bind(&CefWallet::OnBeforeNavigate, this, std::placeholders::_1, std::placeholders::_2));
	cef_control_->AttachLoadingStateChange(nbase::Bind(&CefWallet::OnLoadingStateChange, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachLoadEnd(nbase::Bind(&CefWallet::OnLoadEnd, this, std::placeholders::_1));
	cef_control_->AttachLoadError(nbase::Bind(&CefWallet::OnLoadError, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
	cef_control_->AttachAfterCreated(nbase::Bind(&CefWallet::OnAfterCreated, this, std::placeholders::_1));
	cef_control_->AttachBeforeCLose(nbase::Bind(&CefWallet::OnBeforeClose, this, std::placeholders::_1));
	cef_control_->AttachBeforeContextMenu(nbase::Bind(&CefWallet::OnBeforeContextMenu, this, std::placeholders::_1, std::placeholders::_2));
	std::wstring html_path = L"https://wallet.metachat.la/#/?token=" + nbase::UTF8ToUTF16(app_sdk::AppSDKInterface::GetInstance()->user_token);
	cef_control_->LoadURL(html_path);
}

void CefWallet::ReloadUrl() {
	cef_control_->Refresh();
}

bool CefWallet::OnMenuCommand(CefRefPtr<CefContextMenuParams> params, int command_id, CefContextMenuHandler::EventFlags event_flags) {
	return false;
}

bool CefWallet::OnLinkClick(const std::wstring& url) {
	return true;
}

CefRequestHandler::ReturnValue CefWallet::OnBeforeNavigate(CefRefPtr<CefRequest> request, bool is_redirect) {
	return RV_CONTINUE;
}

void CefWallet::OnLoadingStateChange(bool isLoading, bool canGoBack, bool canGoForward) {
}

void CefWallet::OnLoadEnd(int httpStatusCode) {
}

void CefWallet::OnLoadError(CefLoadHandler::ErrorCode errorCode, const CefString& errorText, const CefString& failedUrl) {
}

void CefWallet::OnAfterCreated(CefRefPtr<CefBrowser> browser) {
}

void CefWallet::OnBeforeClose(CefRefPtr<CefBrowser> browser) {
}

void CefWallet::OnBeforeContextMenu(CefRefPtr<CefContextMenuParams> params, CefRefPtr<CefMenuModel> model) {
	model->Clear();
}